﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


/* 原文链接：https://blog.csdn.net/aa2528877987/article/details/119621484 */
namespace Wpf_Datagrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void win_Loaded(object sender, RoutedEventArgs e)
        {
            // 登录窗口显示了
            // MessageBox.Show("lalal");
            //初始化DGVModel
            //DGVModel vmodel = new DGVModel();
            //vmodel.UserList = GetUserList();
            //vmodel.DeptList = GetDepts();
            //this.DataContext = vmodel;

            // 给表格赋值
            dgList.ItemsSource = GetUserList();

        }

        // 按钮点击事件
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("click");
            System.Diagnostics.Debug.WriteLine("点击编辑: " + sender);

            System.Diagnostics.Debug.WriteLine("点击编辑2: " + e);

            System.Diagnostics.Debug.WriteLine("点击编辑2-this: " + this);

        }

        /// 获取用户信息列表
        private List<UserInfoNew> GetUserList()
        {
            List<UserInfoNew> list = new List<UserInfoNew>();
            /*string sql = "select UserId,UserName,UserState,UserAge,DeptId from UserInfos where DeptId>0";
            SqlDataReader dr = SqlHelper.ExecuteReader(sql, 1);*/
            String[] nameArr = { "张三", "李四", "王五", "赵六", "陈七" };
            for (int i = 0; i < 5; i++)
            {
                UserInfoNew user = new UserInfoNew();
                user.UserId = i;
                user.UserName = nameArr[i];
                user.UserState = (i % 2 == 0) ? true : false;
                user.UserAge = i;
                user.DeptId = i;
                list.Add(user);
            }
            return list;
        }


        /// 获取部门
        private List<DeptInfo> GetDepts()
        {
            List<DeptInfo> list = new List<DeptInfo>();
            for (int i = 0; i < 3; i++)
            {
                DeptInfo dept = new DeptInfo();
                dept.DeptId = i;
                dept.DeptName = i + "";
                list.Add(dept);
            }
            return list;
        }

        private void New_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var aa = e.Parameter.ToString();
            MessageBox.Show(aa);
            System.Diagnostics.Debug.WriteLine("click: " + e.Parameter.ToString());
        }

        private void New_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }

    public class UserInfoNew
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public bool UserState { get; set; }
        public int UserAge { get; set; }
        public int DeptId { get; set; }

        private CommandBinding _detailCommand;

        public CommandBinding getData
        {
            get
            {
                if (_detailCommand==null)
                {
                    _detailCommand = new CommandBinding();
                }
                return _detailCommand;
            }
        }

        private void showData(object obj)
        {

        }


        
    }

    public class DeptInfo
    {
        public int DeptId { get; set; }
        public string DeptName { get; set; }
    }

    /// <summary>
    /// Window的数据上下文
    /// </summary>
    public class DGVModel
    {
        public List<UserInfoNew> UserList { get; set; }
        public List<DeptInfo> DeptList { get; set; }
    }
    /*
     * 原文链接：https://blog.csdn.net/aa2528877987/article/details/119621484
     */
}


